import router from '../../router/index'

export default {
  state: {
    VuexTagsList: [],
  },
  mutations: {
    // 关闭单个标签
    closeTags(state, playold) {
      const delItem = state.VuexTagsList.splice(playold.index, 1)[0];
      const item = state.VuexTagsList[playold.index] ? state.VuexTagsList[playold.index] : state.VuexTagsList[playold.index - 1];
      if (item) {
        delItem.path === playold.full && router.push(item.path);
      } else {
        router.push('/');
      }
    },
    // 关闭全部标签
    closeAll(state) {
      state.VuexTagsList.splice(0,state.VuexTagsList.length);
      router.push('/');
    },
    // 关闭其他标签
    closeOther(state,playold) {
      const curItem = state.VuexTagsList.filter(item => {
        return item.path == playold;
      });
      state.VuexTagsList.splice(0,state.VuexTagsList.length);
      state.VuexTagsList.push(curItem[0])
    },
    // 设置标签
    // 标签的meta属性在路由中设置,
    setTags(state, route) {
      const isExist = state.VuexTagsList.some(item => {
        return item.path === route.fullPath;
      });
      if (!isExist) {
        if (state.VuexTagsList.length >= 8) {
          state.VuexTagsList.shift();
        }
        state.VuexTagsList.push({
          title: route.meta.title,
          path: route.fullPath,
          name: route.matched[1].components.default.name
        });
        // console.log(state.VuexTagsList);
      }
    },
    // 监听遍历
    VueXForTags(state) {
      for (let i = 0; i < state.VuexTagsList.length; i++) {
        for (let x = i + 1; x < state.VuexTagsList.length; x++) {
          if (state.VuexTagsList[i].name == state.VuexTagsList[x].name) {
            state.VuexTagsList.splice(i, 1);
          }
        }
      }
    },
    // 删除指定
    VueXSplice(state,playlod){
      state.VuexTagsList.spalice(playlod,1)
    },
    // 停止计划退出删除指定
    VueXDeleteTags(state,playlod){
      for(let i in state.VuexTagsList){
        state.VuexTagsList[i].path.indexOf('?') != -1 ? state.VuexTagsList[i].path = state.VuexTagsList[i].path.slice(0,state.VuexTagsList[i].path.indexOf('?')) : state.VuexTagsList[i].path;
        if(state.VuexTagsList[i].path == playlod){
          state.VuexTagsList.splice(i,1)
        }
      }
    }
  }
}